package com.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dto.AdminLoginDto;
import com.dto.AdminSaveDto;
import com.dto.UserLoginDto;
import com.exceptions.DataBaseException;
import com.exceptions.LoginFailedException;
import com.exceptions.UpLoadFailedException;
import com.mapper.UserMapper;
import com.po.User;
import com.service.UserService;
import com.utils.AliOssUtil;
import com.vo.AdminLoginVo;
import com.vo.UserLoginVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    private final UserMapper userMapper;

    private final AliOssUtil aliOssUtil;

    @Override
    public UserLoginVo login(UserLoginDto userLoginDto) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .eq(User::getName, userLoginDto.getName())
                .eq(User::getPassword, userLoginDto.getPassword());
        User user = userMapper.selectOne(wrapper);
        if (user.getStatus()==0){
            throw new LoginFailedException("该用户账号异常，请联系管理员！");
        }
        if (ObjectUtil.isEmpty(user)) {
            throw new LoginFailedException("用户名或者密码错误！");
        }
        log.info("获取到用户：{}",user);
        StpUtil.login(user.getId());
        UserLoginVo userLoginVO=new UserLoginVo();
        BeanUtils.copyProperties(user,userLoginVO);
        userLoginVO.setToken(StpUtil.getTokenValue());
        return userLoginVO;
    }

    @Override
    public void uploadAvatar(MultipartFile file) {
        //将文件传入oss
        long userid = StpUtil.getLoginIdAsLong();
        log.info("用户上传图片用户id:{}",userid);
        String filePath;
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            String extention = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectname = UUID.randomUUID().toString() + extention;
            filePath = aliOssUtil.upload(file.getBytes(), objectname);
        }catch (Exception e){
            throw new UpLoadFailedException("文件上传失败！");
        }
        //存入数据库
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, userid);
        User user = new User();
        user.setImage(filePath);
        this.update(user, queryWrapper);
    }

    @Override
    public void changeName(String name) {
        //存入数据库
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,StpUtil.getLoginIdAsLong());
        User user = new User();
        user.setName(name);
        try{
            this.update(user, queryWrapper);
        }catch (Exception e){
            throw new DataBaseException("该用户名已存在！请更换用户名qwq");
        }

    }


}
